export {}
require('json5/lib/register')
const gulp = require('gulp')
const JSON5 = require('json5')
const fs = require('fs')
const del = require('rimraf')
const _ = require('lodash')
const log = require('fancy-log')
const Path = require('path')
const mkdirp = require('mkdirp')
const glob = require('glob')
const through = require('through2')
const Mustache = require('mustache')
const uuid = require('uuid/v1')
const newer = require('gulp-newer')

let {toPath,getFullPath} = require('sulp_utils')

let buildDir=''
let incDirList=[]

module.exports = {
  setBuildDir: (s)=>{
    buildDir = toPath(s,'lib','nc','15.2')
  },

  addIncDir: (s)=> {
    incDirList.push(s)
  },

  scTarget: (args)=> {
    const NC_LIBRARY_DIR= buildDir
    if(!fs.existsSync(NC_LIBRARY_DIR)) {
      mkdirp.sync(NC_LIBRARY_DIR)
    }
    const target=toPath(NC_LIBRARY_DIR,args.lib_name)
    return target
  },

  scTargetDir: ()=> {
    return buildDir
  },

  linkSC: (args)=> {
    const target          = args.target+'.so'
    const CDS_INST_DIR    = process.env.CDS_INST_DIR
    const ius_lib_path    = `${CDS_INST_DIR}/tools/systemc/lib/64bit/gnu`
    const obj_list        = args.obj_list
    const static_lib_list = args.static_lib_list

    const ret=['-m64','-Wl,-G','-shared','-o']
    ret.push(target)
    ret.push(`${ius_lib_path}/libncscCoSim_sh.so`)
    ret.push(`${ius_lib_path}/libncscCoroutines_sh.so`)
    ret.push(`${ius_lib_path}/libsystemc_sh.so`)
    ret.push("-ljsoncpp")
    if(obj_list.length>0) {
      ret.push(...obj_list) 
    }
    if(static_lib_list.length>0) {
      ret.push(...static_lib_list)
    }
    return {
      cmd: 'gcc',
      args: ret,
      target: target,
      cwd: buildDir,
    }
  },

  compileSC: (args)=> {
    const SC_INC_DIR=_.join(_.map(incDirList,(i)=>'-I'+i),' ')
    const CDS_INST_DIR=process.env.CDS_INST_DIR
    const TOOL_INC_DIR=`${SC_INC_DIR} -I${toPath(CDS_INST_DIR,'tools','include')}`
    const ret_args= [
      '-64bit',
      '-NOSTDOUT',
      '-cflags',
      `${TOOL_INC_DIR} -fPIC`,
      args.file,
    ]
    const cwd = buildDir
    const target=cwd+'/'+Path.basename(args.file,'.cpp')+'.o'
    return {
      cmd: 'ncsc',
      args: ret_args,
      target: target,
      cwd: cwd,
    }
  },
}

